<template>
  <div class="article" :style="{marginLeft: '20rem'}">
    <div class="markdown-body" v-html="content" />
    <div class="toc-container animate-zoom-inOut" :class="{'maximize': maximize, 'minimize': minimize}">
      <transition name="fade">
        <div v-show="minimize" class="toc-trigger" :style="{ 'top': topOffset}" @click="maximize = true">
          <svg-icon icon-class="toc" />
        </div>
      </transition>
      <transition name="fade">
        <div v-show="maximize" class="markdown-toc">
          <header class="flex items-center">
            <svg-icon icon-class="toc" class-name="toc-icon" />

            <h2>Contents</h2>
            <i class="el-icon-close toc-close" @click="maximize = false" />

          </header>
          <el-scrollbar class="scrollbar">
            <ul :style="{height: contentHeight}">
              <li
                v-for="item in toc"
                :key="item.id"
                class="sub-toc"
                :style="{ 'padding-left': item.level + 'em', 'font-weight': item.level <= 2 ? 'bold' : '400'}"
                @click="scrollToTitle(item.id)"
                v-html="item.label"
              />
            </ul>
          </el-scrollbar>
        </div>
      </transition>
    </div>
  </div>
</template>

<script>
import { scrollToTargetAdjusted } from '@/utils'
import '@/styles/github-markdown-light.css'

export default {
  props: {
    content: String,
    toc: Array
  },
  data() {
    return {
      msg: '',
      maximize: false,
      contentHeight: 400 + 'px',
      topOffset: document.body.clientHeight / 2 + 'px'
    }
  },
  computed: {
    minimize() {
      return !this.maximize
    }
  },
  mounted() {
    window.addEventListener('resize', this.resizeHanlder)
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.resizeHanlder)
  },
  methods: {
    scrollToTitle(id) {
      scrollToTargetAdjusted(id, 60)
    },
    resizeHanlder() {
      this.contentHeight = document.body.clientHeight - 60 + 'px'
      this.topOffset = document.body.clientHeight / 2 + 'px'
    }
  },
  beforeRouteEnter(from, to, next) {
    next()
  }
}
</script>

<style lang="scss" scoped>
.markdown-body {
  min-height: 100vh;
  padding: 20px 40px;
}

.toc-trigger {
  border: 2px solid rgba(51,51,51,0.95);
  background: rgba(243,243,243,0.95);
  top: 200px;
  width: 50px;
  height: 50px;
  right: 0px;
  font-size: 40px;
  cursor: pointer;
  position: fixed;
  display: inline-block;
  border-radius: 7px;
  svg {
    vertical-align: baseline;
    width: 48px;
    height: 48px;

  }
}

.animate-zoom-inOut.maximize {
  .toc-trigger {
     animation: tocHide .3s ease-out forwards
  }

  .markdown-toc {
     animation: tocZoomIn .3s ease-out forwards
  }
}

.animate-fade-in.maximize {

}

.animate-zoom-inOut.minimize {
  .toc-trigger {
     animation: tocShow .3s forwards
  }

  .markdown-toc {
    animation: tocZoomOut .4s ease-in forwards;
  }
}

.animate-zoomIn {

}

.markdown-toc {
  position: fixed;
  top: 60px;
  right: 0;
  min-width: 300px;
  overflow-y: hidden;
  overflow-x: hidden;
  z-index: 1;
  padding: 10px;

  background: rgba(243,243,243,0.95);
  // box-shadow: 0 0 1em #777777;
  border: 2px solid rgba(51,51,51,0.95);
  transform-origin: right center;

  ul {
    height: 80%;
  }

  h2 {
    font-size: 22px;
    border: 0;
    font-weight: bold;
    // padding-left: 0.5em;
    padding-top: 0.05em;
    padding-bottom: 0.05em;
    flex: 1 1;
  }

  .toc-icon {
    font-size: 40px;
    float: left;

  }

  .toc-close {
    float: right;
    font-weight: bold;
    margin-left: 5px;
    opacity: .5;
    font-size: 24px;
    text-align: center;
  }

  .sub-toc {
    font-size: 14px;
    padding: 5px;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial,
      sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji';
    color: #606266;
    cursor: pointer;
  }
  .sub-toc:hover {
    color: #0969da;
  }
}

.article {
  width: calc(100% - 20rem);
}

.fade-enter-active {
  animation: tocFadeIn .5s;
}

@keyframes tocHide {
    0% {
        visibility: visible
    }

    to {
        visibility: hidden
    }
}

@keyframes tocShow {
    0%,99% {
        visibility: hidden
    }

    to {
        visibility: visible
    }
}

@keyframes tocFadeIn {
      0% {
        opacity: 0;
        visibility: hidden
    }

    to {
        opacity: 1;
        visibility: visible
    }
}

@keyframes tocFadeOut {
    0% {
        opacity: 1;
        visibility: visible
    }

    to {
        opacity: 0;
        visibility: hidden
    }
}

@keyframes tocZoomIn {
    0% {
        -webkit-transform: scale(0);
        -ms-transform: scale(0);
        transform: scale(0);
        visibility: hidden;
        opacity: 0
    }

    20% {
        opacity: 0
    }

    30% {
        opacity: 1
    }

    to {
        transform: scale(1);
        visibility: visible;
        opacity: 1
    }
}

@keyframes tocZoomOut {
    0% {
        transform: scale(1);
        visibility: visible;
        opacity: 1
    }

    50% {
        opacity: 1
    }

    80% {
        opacity: .3
    }

    to {
        transform: scale(0);
        visibility: hidden;
        opacity: 0
    }
}
</style>
